﻿Imports System.Data.OleDb

Public Class CricGroundDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            BindComments()
        End If
    End Sub
    Protected Sub btnAddComment_Click(sender As Object, e As EventArgs) Handles btnAddComment.Click
        Response.Redirect("AddToBlog/AddCommentCricketGround.aspx?ID=" + Request.QueryString("ID"))
    End Sub
    Private Sub BindComments()


        Dim sqlQueryForeRole As String = "SELECT CricketGroundComments.ID, CricketGroundComments.GroundPicture, CricketGroundComments.Title, CricketGroundComments.UserComments, CricketGroundComments.CreatedBy,CricketGroundComments.CreatedDate, Count(CricketGroundCommentLikes.CGCommentID) AS Likes FROM CricketGroundComments LEFT JOIN CricketGroundCommentLikes ON CricketGroundComments.ID = CricketGroundCommentLikes.CGCommentID WHERE (((CricketGroundComments.GroundID)=[@f1]))  GROUP BY CricketGroundComments.ID, CricketGroundComments.GroundPicture, CricketGroundComments.Title, CricketGroundComments.UserComments, CricketGroundComments.CreatedBy,CricketGroundComments.CreatedDate;"
        Dim p1 As New OleDbParameter("@f1", Request.QueryString("ID"))
        Dim ds = DataAccessLayer.ExecuteDataSet(sqlQueryForeRole, p1)
        Session.Add("myTable", ds.Tables(0))
        gvComments.DataSource = ds
        gvComments.DataBind()
    End Sub

    Protected Sub gvComments_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvComments.RowDeleting
        Dim s As String = String.Empty
        Dim i As Integer
        If (User.Identity.Name.Length > 0) Then
            Dim id As String = gvComments.DataKeys(e.RowIndex).Value.ToString
            Dim p1 As New OleDbParameter("@f1", id)
            Dim usernameP As New OleDbParameter("@f2", User.Identity.Name)
            Using oledbReader As OleDbDataReader = DataAccessLayer.ExecuteDataReader("Select count(*) from CricketGroundCommentLikes where CGCommentID= @f1 and Username=@f2", p1, usernameP)
                While oledbReader.Read
                    If Not IsDBNull(oledbReader.Item(0)) Then
                        i = Convert.ToInt32(oledbReader.Item(0))
                    End If
                End While
            End Using


            If i = 0 Then
                Dim sql2 As String = "insert into CricketGroundCommentLikes (CGCommentID,Username,UserLike) values (@f1,@f2,@f3)"
                Dim p3 As New OleDbParameter("@f3", "True")
                DataAccessLayer.ExecuteNonQuery(sql2, p1, usernameP, p3)
                Dim sql21 As String = "Update userprofile set CommentsLikedByOthers= CommentsLikedByOthers+1 where username =@f2"
                DataAccessLayer.ExecuteNonQuery(sql21, usernameP)
            Else
                Dim sql1 As String = "Delete * from CricketGroundCommentLikes where CGCommentID=@f1 and Username=@f2"
                DataAccessLayer.ExecuteNonQuery(sql1, p1, usernameP)

                Dim sql21 As String = "Update userprofile set CommentsLikedByOthers= CommentsLikedByOthers-1 where username =@f2"
                DataAccessLayer.ExecuteNonQuery(sql21, usernameP)

            End If
            BindComments()
        Else
            Response.Redirect("~/Account/Login.aspx")
        End If

    End Sub

    Protected Sub gvComments_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvComments.Sorting
        Dim sortExpression As String = e.SortExpression
        If GridViewSortDirection = SortDirection.Ascending Then
            GridViewSortDirection = SortDirection.Descending
            SortGridView(sortExpression, SortDirection.Descending)
        Else
            GridViewSortDirection = SortDirection.Ascending
            SortGridView(sortExpression, SortDirection.Ascending)
        End If
    End Sub

    Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)
        If direction = "1" Then
            direction = "ASC"
        Else
            direction = "DESC"
        End If
        Dim dv As New DataView(Session("myTable"))
        dv.Sort = sortExpression + " " + direction
        gvComments.DataSource = dv
        gvComments.DataBind()

    End Sub

    Public Property GridViewSortDirection() As SortDirection
        Get
            If ViewState("sortDirection") Is Nothing Then
                ViewState("sortDirection") = SortDirection.Ascending
            End If
            Return DirectCast(ViewState("sortDirection"), SortDirection)
        End Get
        Set(ByVal value As SortDirection)
            ViewState("sortDirection") = value
        End Set
    End Property
End Class